# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  
  def subcanvas(title, controller, customer, folder_open=false, subpartial=nil)
    if subpartial.nil?
      subpartial = controller
    end
    render :partial => 'common/subcanvas', :locals => {:customer => customer, :controller => controller, :subpartial => subpartial, :title => title, :folder_open => folder_open}
  end
  
  def icon_edit(controller, id, customer=nil)
    if authorized?(controller, 'edit')
      if customer.nil?
        link_to_remote(image_tag('pencil.png', :title => 'Muokkaa'), :url => self.send('edit_' + controller.singularize + '_url', id), :before => "spinnerOn('#{controller}');", :complete => "spinnerOff('#{controller}');", :method => :get)
      else
        link_to_remote(image_tag('pencil.png', :title => 'Muokkaa'), :url => self.send('edit_customer_' + controller.singularize + '_url', customer, id), :before => "spinnerOn('#{controller}');", :complete => "spinnerOff('#{controller}');", :method => :get)
      end
    end
  end
  
  def icon_delete(controller, id, message='Oletko varma?', action='destroy', customer=nil)
    if authorized?(controller, action)
      if customer.nil?
        link_to_remote image_tag('cross.png', :title => 'Poista'), :url => self.send(controller.singularize + '_url', id), :confirm => message, :before => "spinnerOn('#{controller}');", :complete => "spinnerOff('#{controller}');", :method => :delete
      end
    end
  end
  
  def checkbox_delete_selected(controller, id)
    if authorized?(controller, 'destroy_selected')
      check_box_tag 'selected[]', id
    end
  end
  
  def button_ok
    button_submit_tag image_tag('tick.png', :size=>'16x16') + 'OK', :class => 'positive'
  end
  
  def button_cancel(controller, url, folder=nil)
    if folder.nil?
      folder = controller
    end
    link_to_remote image_tag('cross.png', :size=>'16x16') + 'Peruuta', :url=> url, :before => "spinnerOn('#{folder}');", :complete => "spinnerOff('#{folder}');", :method => :get
  end
  
  def toolbar_delete_selected(controller=nil)
    if authorized?(controller, 'destroy_selected')
      content_tag 'li', link_to_function(image_tag('bin.png', :title => 'Poista valitut'), "$('#{controller}_form').onsubmit();")
    end 
  end
  
  def toolbar_separator
    content_tag 'li', link_to(image_tag('separator.png', :title => ''), '#', :class => 'separator')
  end
  
  def toolbar_add(desc, controller, id=nil)
    if authorized?(controller, 'new')
      if id.nil?
        content_tag 'li', link_to_remote(image_tag("add.png", :title => 'Lisää ' + desc), :url => self.send('new_' + controller.singularize + '_url'), :before => "spinnerOn('#{controller}');", :complete => "spinnerOff('#{controller}');", :method => :get)
      else
        content_tag 'li', link_to_remote(image_tag("add.png", :title => 'Lisää ' + desc), :url => self.send('new_customer_' + controller.singularize + '_url', id), :before => "spinnerOn('#{controller}');", :complete => "spinnerOff('#{controller}');", :method => :get)
      end
    end  
  end
  
  def toolbar_edit(desc, controller, id, customer=nil, folder=nil)
    if folder.nil?
      folder = controller
    end
    if authorized?(controller, 'edit')
      if(customer.nil?)
        content_tag 'li', link_to_remote(image_tag("pencil.png", :title => 'Muokkaa ' + desc), :url => self.send('edit_' + controller.singularize + '_url', id), :before => "spinnerOn('#{folder}');", :complete => "spinnerOff('#{folder}');", :method => :get)
      else
        content_tag 'li', link_to_remote(image_tag("pencil.png", :title => 'Muokkaa ' + desc), :url => self.send('edit_customer_' + controller.singularize + '_url', customer, id), :before => "spinnerOn('#{folder}');", :complete => "spinnerOff('#{folder}');", :method => :get)
      end
    end
  end
  
  def toolbar_next(controller, id)
    if !id.nil?
      content_tag 'li', link_to_remote(image_tag("resultset_next.png", :title => "Seuraava"), :url => {:controller => controller, :action => 'list'}, :id => @customer, :next => id), :before => "spinnerOn('#{controller}');", :complete => "spinnerOff('#{controller}');"
    end
  end
  
  def toolbar_previous(controller, id)
    if !id.nil?
      content_tag 'li', link_to_remote(image_tag("resultset_previous.png", :title => "Edellinen"), :url => {:controller => controller, :action => 'list'}, :id => @customer, :previous => id), :before => "spinnerOn('#{controller}');", :complete => "spinnerOff('#{controller}');"
    end
  end
  
  def button_delete_selected(controller=nil)
    if authorized?(controller, 'destroy_selected')
      button_submit_tag image_tag("delete.png", :size => "16x16") + 'Poista valitut', :class => "negative"
    end
  end
  
  def button_add(desc, controller, id=nil, folder=nil)
    if authorized?(controller, 'new')
      if id.nil?
        link_to_remote(image_tag("add.png", :size => "16x16") + 'Lisää ' + desc, :url => { :controller => controller, :action => 'new'}, :before => "spinnerOn('#{controller}');", :complete => "spinnerOff('#{controller}');")
      else
        link_to_remote(image_tag("add.png", :size => "16x16") + 'Lisää ' + desc, :url => { :controller => controller, :action => 'new', :id => id}, :before => "spinnerOn('#{controller}');", :complete => "spinnerOff('#{controller}');")
      end
    end
  end
  
  
  def button_edit(desc, controller, id, folder=nil)
    if folder.nil?
      folder = controller
    end
    if authorized?(controller, 'edit')
      link_to_remote(image_tag('pencil.png', :size => '16x16') + 'Muokkaa ' + desc, :url => self.send('edit_' + controller.singularize + '_url', id), :before => "spinnerOn('#{folder}');", :complete => "spinnerOff('#{folder}');", :method => :get)
    end
  end
  
  def link_to_remote(name, options = {}, html_options = {})
    html_options.merge!({:href => url_for(options[:url])}) unless options[:url].blank?
    super(name, options, html_options)
  end
end
